package towa.com.remoteWMP;

import android.util.Log;

public class ProgressBarUpdateThread extends Thread {
	
	private RemoteWMPActivity activity;
	private int progress;
	private boolean isUpdating;
	private boolean waiting;
	private boolean running;
	
	public ProgressBarUpdateThread(RemoteWMPActivity activity) {
		this.activity = activity;
		isUpdating = false;
		waiting = false;
		running = true;
	}
	
	@Override
	public void run() {
		progress = 0;
		while (running) {
			while (isUpdating) {
				progress = activity.getProgressProgress() + 1;
				activity.updateProgressBar(progress);
				synchronized (this) {
					try {
						wait(1000);
//						Log.i("WMPRemoteUpdateThread", progress + "");
					} catch (InterruptedException e) {
						Log.e("WMPRemoteUpdateThread", e.toString());
					}
				}
			}
		}
	}
	
	public void setProgress(int progress) {
		this.progress = progress;
	}

	public void begin(boolean b) {
		isUpdating = b;
	}
	
	public void stopThread() {
		progress = 0;
		if (activity.isPlay()) {
			isUpdating = true;
		} else {
			isUpdating = false;
		}
		activity.updateProgressBar(progress);
	}
	
	public void restart() {
		progress = 0;
		if (!activity.isPlay()) {
			isUpdating = true;
		}
		activity.updateProgressBar(progress);
	}
	
}
